package com.pandas.community.service;

import com.pandas.community.Utils.SensitiveFilter;
import com.pandas.community.dao.MessageMapper;
import com.pandas.community.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;
import org.unbescape.html.HtmlEscape;

import java.util.List;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<Message> selectConversations(int userid, int offset, int limit){
        return messageMapper.selectConversations(userid, offset, limit);
    }


    public int selectConversationsCount(int userid){
        return messageMapper.selectConversationsCount(userid);
    }


    public List<Message> selectLetters(String conversationId, int offset, int limit){
        return messageMapper.selectLetters(conversationId, offset, limit);
    }


    public int selectLettersCount(String conversationId){
        return messageMapper.selectLettersCount(conversationId);
    }


    public int selectUnReadLettersCount(int userid, String conversationId){
        return messageMapper.selectUnReadLettersCount(userid, conversationId);
    }

    //新增消息
    public int insertMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    //修改消息状态
    public int readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids, 1);
    }

    //查询某个Topic下的最新通知
    public Message getLatestNotice(int userid, String topic){
        return messageMapper.selectLatestNotice(userid, topic);
    }

    //查询某个主题所包含的通知数量
    public int selectNoticeCount(int userid, String topic){
        return messageMapper.selectNoticeCount(userid, topic);
    }

    //查询未读的通知的数量
    public int selectUnreadNoticeCount(int userid, String topic){
        return messageMapper.selectUnreadNoticeCount(userid, topic);
    }

    public List<Message> selectNotice(int userid, String topic, int offset, int limit){
        return messageMapper.selectNotice(userid, topic, offset, limit);
    }
}
